using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace OrgStructure.Features.OrgStructure
{
    /// <summary>
    /// Этот класс обрабатывает события, возникающие в ходе активации, деактивации, установки, удаления и обновления компонентов.
    /// </summary>
    /// <remarks>
    /// GUID, присоединенный к этому классу, может использоваться при создании пакета и не должен изменяться.
    /// </remarks>

    [Guid("fa1d3bd1-0b8b-4c33-8ce0-b02569ed423c")]
    public class OrgStructureEventReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (job.Name == "UpdateOrgStructure"
                            || job.Name == "UpdateOrgStructure_v_1_2"
                            || job.Name == "UpdateOrgStructure_v_1_3"
                            || job.Name == "UpdateOrgStructure_v_1_4"
                            || job.Name == "UpdateOrgStructure_v_1_5"
                            )
                        {
                            job.Delete();
                        }
                    }

                    JobOrgStructure listLoggerJob =
                        new JobOrgStructure("UpdateOrgStructure_v_1_5", site.WebApplication);
                    SPDailySchedule schedule =
                        new SPDailySchedule();

                    schedule.BeginHour = 1;
                    schedule.BeginMinute = 0;
                    schedule.BeginSecond = 0;
                    schedule.EndHour = 1;
                    schedule.EndMinute = 59;
                    schedule.EndSecond = 59;
                    listLoggerJob.Schedule = schedule;
                    listLoggerJob.Update();
                }
            });
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (job.Name == "UpdateOrgStructure"
                            || job.Name == "UpdateOrgStructure_v_1_2"
                            || job.Name == "UpdateOrgStructure_v_1_3"
                            || job.Name == "UpdateOrgStructure_v_1_4"
                            || job.Name == "UpdateOrgStructure_v_1_5"
                            )
                        {
                            job.Delete();
                        }
                    }
                }
            });
        }

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

    }
}
